import pandas as pd
from elasticsearch import Elasticsearch, helpers

from bqtool.test.tvcfile_to_mongodb import df_to_mongodb, MongoBase


def get_es(index,doc_type):
    es_client = Elasticsearch(['10.2.111.51'], http_auth=('admin', 'admin'), port=9200)
    start = pd.Timestamp(startTime).tz_localize('Asia/Shanghai')
    end = pd.Timestamp(endTime).tz_localize('Asia/Shanghai')
    query_json = {
        'query': {
            'bool': {
                'must': [
                    {'range': {'gcsj': {  # 过车时间
                        'gte': int(start.value / 10 ** 6),
                        'lt': int(end.value / 10 ** 6),
                    }
                    }}
                ],
            }
        }
    }
    # all_Doc = helpers.scan(client=es_clent, scroll='5m', index='ehlindex201812', doc_type='pass_car',query=query_json)
    all_Doc = helpers.scan(client=es_client, scroll='2m', index=index, doc_type=doc_type,query=query_json)
    return pd.DataFrame([i['_source'] for i in all_Doc])


if __name__ == '__main__':
    startTime = '2018-05-11 11:10:00'
    endTime = '2018-05-11 11:12:00'
    df = get_es('ehlindex201805','pass_car')
    df_to_mongodb(df,MongoBase('test',db='test'))
    print('ok')